const router = require('koa-router')();
const cc = require('../conCtl');
const js = require('../jsCtl');
const db = require('../dbCtl');


/**
@hostpath = http://127.0.0.1/service/banner
 */


/** 获取banner列表
###
    GET {{hostpath}}?page=1&size=30
### */
router.get('/', async c => {
    let
        page = c.query.page || 1,
        size = c.query.size || 30;

    let model = db.model('t_banner').where('state>0');

    let data = await model.orderBy('dt desc').select(page, size);
    let count = await model.count();

    cc.send_list(c, data, count);
});


/** 新增banner图
###
    POST {{hostpath}}/add
    Content-Type: application/json

    {
        "id":3,
        "name":"banner图3",
        "img":"https://thirdwx.qlogo.cn/mmpen/vi_32/Q0j4TwGTfTJV1PtYU4Hy5XscbWPHvic2RoSJqIuXbYK3FKtISqGAvcwRy9qFRvuiaictNYsEOG1NYibwNpNFqTMAVg/132;https://thirdwx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEJKiajvARyJrQUGeybA1WHGs88U7vUWvqtq0Ue3ibDjG35fseCR7z7rjw9lkhiaLbETbxfvHOWuk6Ticw/132;https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoGCV7UU01Bdeib9uzU3nqKibobLe80xbpqMFeSu2F6kEYcx2jicbyYwo8wOLibR0oHmEWad8WFDVwR1w/132;https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKsVHo3GJm61NcTZqnBGZwkfTjHaJA963QNNEuPjibvK6YpUV29K51UqHGpwWSvqp90vl8V60SKicWA/132",
        "state":2
    }
### */
router.post('/add', async c => {
    js.check_params(
        !c.request.body.name && '?banner标题' ||
        !c.request.body.img && '?banner图片');

    let data = c.request.body;
    if (!c.request.body.id) delete data.id;
    
    await db.model('t_banner').insertOrUpdate(data);

    cc.send(c);
});

/** 删除banner图
###
    GET {{hostpath}}/del?id=3
### */
router.get('/del', async c => {
    js.check_params(!c.query.id && '?banner图ID');

    let data = c.query;
    
    await db.model('t_banner').where('id=?', data.id).relyUpdate('删除失败',{
        state: 0
    });

    cc.send(c);
});

/** 获取邀请奖品数据
###
  GET {{hostpath}}/prize
### */
router.get('/prize', async c => {
    
    let data = await db.model('t_invitation').shift('未找到指定奖品');
    cc.send_list(c, data);
});


/** 修改邀请奖品数据
###
    POST {{hostpath}}/upd
    Content-Type: application/json

    {
        "prize":"iPhone 11",
        "img":"https://thirdwx.qlogo.cn/mmpen/vi_32/Q0j4TwGTfTJV1PtYU4Hy5XscbWPHvic2RoSJqIuXbYK3FKtISqGAvcwRy9qFRvuiaictNYsEOG1NYibwNpNFqTMAVg/132;https://thirdwx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEJKiajvARyJrQUGeybA1WHGs88U7vUWvqtq0Ue3ibDjG35fseCR7z7rjw9lkhiaLbETbxfvHOWuk6Ticw/132;https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoGCV7UU01Bdeib9uzU3nqKibobLe80xbpqMFeSu2F6kEYcx2jicbyYwo8wOLibR0oHmEWad8WFDVwR1w/132;https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKsVHo3GJm61NcTZqnBGZwkfTjHaJA963QNNEuPjibvK6YpUV29K51UqHGpwWSvqp90vl8V60SKicWA/132"
    }
### */
router.post('/upd', async c => {
    js.check_params(
        !c.request.body.prize && '?邀请奖品名称' ||
        !c.request.body.img && '?邀请奖品图片');

    let data = c.request.body;
    await db.model('t_invitation').where('1=1').relyUpdate('修改失败',data);

    cc.send(c);
});



module.exports = router;